package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.models.mwsreportbean.MwsInventoryReconciliation;
import com.yunchang.springboot.models.mwsreportbean.MwsInventoryReconciliationDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.ToolUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author czw
 */
@Component
public class InventoryReconciliationDataDispose extends ReportDataDisposeParent implements IReportDataDispose {
    /**
     * 库存调整  将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopBrandName
     * @param area
     * @return
     * @throws Exception
     */
    @Override
    public List<Object> reportDataDispose(List<List<String>> lists, String shopBrandName, String area,String sellerId) throws Exception {
        List<Object> insertList = new ArrayList<>(lists.size());
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i), i);
        }
        // 不用区域的表头有所不同，目前是日本区域的不同需要做额外处理
        if (null != area && area.equals(ESPECIALLY_AREA)) {
            int fnskuIndex = Integer.valueOf(String.valueOf(titleMap.get("Fulfillment Network SKU")));
            int skuIndex = Integer.valueOf(String.valueOf(titleMap.get("Merchant SKU")));
            int asinIndex = Integer.valueOf(String.valueOf(titleMap.get("ASIN")));
            int productNameIndex = Integer.valueOf(String.valueOf(titleMap.get("Title")));
            int conditionIndex = Integer.valueOf(String.valueOf(titleMap.get("Condition")));
            int beginningQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("beginning-quantity")));
            int endingQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("ending-quantity")));
            int receivedIndex = Integer.valueOf(String.valueOf(titleMap.get("Received")));
            int returnedIndex = Integer.valueOf(String.valueOf(titleMap.get("FBA customer returns")));
            int foundIndex = Integer.valueOf(String.valueOf(titleMap.get("found")));
            int soldIndex = Integer.valueOf(String.valueOf(titleMap.get("sold")));
            int removedIndex = Integer.valueOf(String.valueOf(titleMap.get("removed")));
            int lostIndex = Integer.valueOf(String.valueOf(titleMap.get("lost")));
            int disposedIndex = Integer.valueOf(String.valueOf(titleMap.get("Inventory disposed of")));
            int otherIndex = Integer.valueOf(String.valueOf(titleMap.get("Other")));
            int discrepantIndex = Integer.valueOf(String.valueOf(titleMap.get("discrepant-quantity")));
            for (int i = 1; i < lists.size(); i++) {
                MwsInventoryReconciliationDO mwsInventoryReconciliation = new MwsInventoryReconciliationDO();
                List<String> row = lists.get(i);
                mwsInventoryReconciliation.setSellerId(sellerId);
                mwsInventoryReconciliation.setArea(area);
                mwsInventoryReconciliation.setAsin(row.get(asinIndex));
                mwsInventoryReconciliation.setFnsku(row.get(fnskuIndex));
                mwsInventoryReconciliation.setSku(row.get(skuIndex));
                mwsInventoryReconciliation.setProductName(row.get(productNameIndex));
                mwsInventoryReconciliation.setGoodsCondition(row.get(conditionIndex));
                mwsInventoryReconciliation.setBeginningQuantity(ToolUtil.valueOfInteger(row.get(beginningQuantityIndex)));
                mwsInventoryReconciliation.setEndingQuantity(ToolUtil.valueOfInteger(row.get(endingQuantityIndex)));
                mwsInventoryReconciliation.setReceived(ToolUtil.valueOfInteger(row.get(receivedIndex)));
                mwsInventoryReconciliation.setReturned(ToolUtil.valueOfInteger(row.get(returnedIndex)));
                mwsInventoryReconciliation.setFound(ToolUtil.valueOfInteger(row.get(foundIndex)));
                mwsInventoryReconciliation.setSold(ToolUtil.valueOfInteger(row.get(soldIndex)));
                mwsInventoryReconciliation.setRemoved(ToolUtil.valueOfInteger(row.get(removedIndex)));
                mwsInventoryReconciliation.setLost(ToolUtil.valueOfInteger(row.get(lostIndex)));
                mwsInventoryReconciliation.setDisposed(ToolUtil.valueOfInteger(row.get(disposedIndex)));
                mwsInventoryReconciliation.setOther(ToolUtil.valueOfInteger(row.get(otherIndex)));
                mwsInventoryReconciliation.setDiscrepantQuantity(ToolUtil.valueOfInteger(row.get(discrepantIndex)));
                mwsInventoryReconciliation.setCreateTime(DateUtil.getDate());
                mwsInventoryReconciliation.setUpdateTime(DateUtil.getDate());
                mwsInventoryReconciliation.setFinanceStatus(1);
                insertList.add(mwsInventoryReconciliation);
            }
            return insertList;
        } else {
            int fnskuIndex = Integer.valueOf(String.valueOf(titleMap.get("fnsku")));
            int skuIndex = Integer.valueOf(String.valueOf(titleMap.get("sku")));
            int asinIndex = Integer.valueOf(String.valueOf(titleMap.get("asin")));
            int productNameIndex = Integer.valueOf(String.valueOf(titleMap.get("product-name")));
            int conditionIndex = Integer.valueOf(String.valueOf(titleMap.get("condition")));
            int beginningQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("beginning-quantity")));
            int endingQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("ending-quantity")));
            int receivedIndex = Integer.valueOf(String.valueOf(titleMap.get("received")));
            int returnedIndex = Integer.valueOf(String.valueOf(titleMap.get("returned")));
            int foundIndex = Integer.valueOf(String.valueOf(titleMap.get("found")));
            int soldIndex = Integer.valueOf(String.valueOf(titleMap.get("sold")));
            int removedIndex = Integer.valueOf(String.valueOf(titleMap.get("removed")));
            int lostIndex = Integer.valueOf(String.valueOf(titleMap.get("lost")));
            int disposedIndex = Integer.valueOf(String.valueOf(titleMap.get("disposed")));
            int otherIndex = Integer.valueOf(String.valueOf(titleMap.get("other")));
            int discrepantIndex = Integer.valueOf(String.valueOf(titleMap.get("discrepant-quantity")));
            for (int i = 1; i < lists.size(); i++) {
                MwsInventoryReconciliationDO mwsInventoryReconciliation = new MwsInventoryReconciliationDO();
                List<String> row = lists.get(i);
                mwsInventoryReconciliation.setSellerId(sellerId);
                mwsInventoryReconciliation.setArea(area);
                mwsInventoryReconciliation.setAsin(row.get(asinIndex));
                mwsInventoryReconciliation.setFnsku(row.get(fnskuIndex));
                mwsInventoryReconciliation.setSku(row.get(skuIndex));
                mwsInventoryReconciliation.setProductName(row.get(productNameIndex));
                mwsInventoryReconciliation.setGoodsCondition(row.get(conditionIndex));
                mwsInventoryReconciliation.setBeginningQuantity(ToolUtil.valueOfInteger(row.get(beginningQuantityIndex)));
                mwsInventoryReconciliation.setEndingQuantity(ToolUtil.valueOfInteger(row.get(endingQuantityIndex)));
                mwsInventoryReconciliation.setReceived(ToolUtil.valueOfInteger(row.get(receivedIndex)));
                mwsInventoryReconciliation.setReturned(ToolUtil.valueOfInteger(row.get(returnedIndex)));
                mwsInventoryReconciliation.setFound(ToolUtil.valueOfInteger(row.get(foundIndex)));
                mwsInventoryReconciliation.setSold(ToolUtil.valueOfInteger(row.get(soldIndex)));
                mwsInventoryReconciliation.setRemoved(ToolUtil.valueOfInteger(row.get(removedIndex)));
                mwsInventoryReconciliation.setLost(ToolUtil.valueOfInteger(row.get(lostIndex)));
                mwsInventoryReconciliation.setDisposed(ToolUtil.valueOfInteger(row.get(disposedIndex)));
                mwsInventoryReconciliation.setOther(ToolUtil.valueOfInteger(row.get(otherIndex)));
                mwsInventoryReconciliation.setDiscrepantQuantity(ToolUtil.valueOfInteger(row.get(discrepantIndex)));
                mwsInventoryReconciliation.setCreateTime(DateUtil.getDate());
                mwsInventoryReconciliation.setUpdateTime(DateUtil.getDate());
                mwsInventoryReconciliation.setFinanceStatus(1);
                insertList.add(mwsInventoryReconciliation);
            }
            return insertList;
        }
    }
}
